package przykladyWykresow;

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

@SuppressWarnings("serial")
public class WykresKolowy extends ChartPanelAbstract {

	private Boolean choose;

	static CategoryDataset dataset1 = null;
	static HashMap<String, Integer> hashmap1 = new LinkedHashMap<String, Integer>();

	static {
		hashmap1.put("Linux", 29);
		hashmap1.put("Mac", 20);
		hashmap1.put("Windows", 100);
	}

	public Boolean get2d() {
		return choose;
	}

	public void set2d(Boolean bol) {
		choose = bol;
	}

	private String title_2d = null;
	private String title_3d = null;
	static DefaultPieDataset dataSet;

	public WykresKolowy(String _title_2d, String _title_3d, Boolean choose) {
		super(chooseChart(_title_2d, choose));
		set2d(choose);
		setTitle(_title_2d, true);
		setTitle(_title_3d, false);
	}

	/** * Creates a sample dataset */

	private static PieDataset createDataset() {
		dataSet = new DefaultPieDataset();
		for (String key : hashmap1.keySet()) {
			dataSet.setValue(key, hashmap1.get(key));
		}
		return dataSet;
	}

	/**
	 * * Creates a chart
	 * 
	 * @param _title
	 */

	public static JFreeChart chooseChart(String _title, Boolean choose2d) {
		if (choose2d) {
			return createChart2d(_title);
		} else {
			return createChart(_title);
		}
	}

	public static JFreeChart createChart(String _title) {

		// This will create the dataset
		PieDataset dataset = createDataset();

		JFreeChart chart = ChartFactory.createPieChart3D(_title, // chart title
				dataset, // data
				true, // include legend
				true, false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;

	}

	/**
	 * * Creates a chart
	 * 
	 * @param _title
	 */

	public static JFreeChart createChart2d(String _title) {

		// This will create the dataset
		PieDataset dataset = createDataset();

		JFreeChart chart = ChartFactory.createPieChart(_title, // chart title
				dataset, // data
				true, // include legend
				true, false);

		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;

	}

	@Override
	public void changeData() {
		this.setChart(chooseChart(getTitle(), get2d()));
	}

	@Override
	public String getTitle() {
		return get2d() ? this.title_2d : this.title_3d;
	}

	@Override
	public void setTitle(String title) {
		if (get2d()) {
			this.title_2d = title;
		} else {
			this.title_3d = title;
		}
	}

	public void setTitle(String title, Boolean choosee) {
		if (choosee) {
			this.title_2d = title;
		} else {
			this.title_3d = title;
		}
	}

	@Override
	public HashMap<String, Integer> getHashMapDataSet() {
		return hashmap1;
	}

	@Override
	public void setHashMapDataSet(HashMap<String, Integer> hashMap) {
		if (hashMap != null) {
			hashmap1 = hashMap;
		}
	}

}
